﻿using Application.Features.Monitors.Commands.Delete;
using Application.Features.Monitors.DTOs;
using Application.Features.Monitors.Queries.Pagination;
using Common.Enums;
using Common.Helpers;
using Common.Hubs;
using Common.Interfaces;
using Common.MessageTemplates;
using Common.Models;
using Common.Services;
using Common.Utils;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.SignalR;
using System.ComponentModel;

namespace WebAPI.Controllers;

/// <summary>
/// 系统监控
/// </summary>
[Description("系统监控")]
[AllowAnonymous]
public class MonitorController : ApiControllerBase
{
    private readonly OnlineUserService _onlineUserService;
    private readonly IHubContext<SignalRHub> _hubContext;
    private readonly ICurrentUserService _currentUserService;

    public MonitorController(
        OnlineUserService onlineUserService,
        IHubContext<SignalRHub> hubContext,
        ICurrentUserService currentUserService)
    {
        _onlineUserService = onlineUserService;
        _hubContext = hubContext;
        _currentUserService = currentUserService;
    }

    /// <summary>
    /// 在线用户
    /// </summary>
    /// <returns></returns>
    [HttpGet("query/online/users")]
    [AllowAnonymous]
    [Description("在线用户")]
    public Result<IEnumerable<OnlineUser>> GetOnlineUsers()
    {
        var data = _onlineUserService.GetOnlineUsers();
       return Result<IEnumerable<OnlineUser>>.Succeed(data);
    }

    /// <summary>
    /// 强制下线
    /// </summary>
    /// <returns></returns>
    [HttpPost("online/user/{username}/forcelogout")]
    [AllowAnonymous]
    [Description("强制下线")]
    public async Task<Result> ForceLogoutAsync(string username)
    {
        var onlineUser = _onlineUserService.GetOnlineUsers().FirstOrDefault(x => x.Username == username);
        ThrowHelper.ThrowIfNotFound(onlineUser == null, "用户已下线");

        Random random = new Random();
        SignalRMessageTemplate signalRMessageTemplate = new SignalRMessageTemplate()
        {
            Id = random.Next(999999999),
            MessageType = MessageType.ForceLogout,
            Content = $"你被踢下线",
            Username = username
        };
        await _hubContext.Clients.User(onlineUser!.UserId!).SendAsync("ReceivePrivateMessage", signalRMessageTemplate.ToJson(true, true));

        return await Result.SucceedAsync();
    }


    /// <summary>
    /// 登录日志
    /// </summary>
    /// <returns></returns>
    [HttpPost("query/login/log/pagination")]
    [AllowAnonymous]
    [Description("登录日志")]
    public async Task<Result<PaginatedData<LoggerOutput>>> LoginLogPaginationQueryAsync(LoggersWithPaginationQuery query)
    {
        var data = await Mediator.Send(query);
        return await Result<PaginatedData<LoggerOutput>>.SucceedAsync(data);
    }

    /// <summary>
    /// 操作日志
    /// </summary>
    /// <returns></returns>
    [HttpPost("query/operation/log/pagination")]
    [AllowAnonymous]
    [Description("操作日志")]
    public async Task<Result<PaginatedData<LoggerOutput>>> OperationLogPaginationQueryAsync(LoggersWithPaginationQuery query)
    {
        var data = await Mediator.Send(query);
        return await Result<PaginatedData<LoggerOutput>>.SucceedAsync(data);
    }

    /// <summary>
    /// 系统日志
    /// </summary>
    /// <returns></returns>
    [HttpPost("query/system/log/pagination")]
    [AllowAnonymous]
    [Description("系统日志")]
    public async Task<Result<PaginatedData<LoggerOutput>>> SystemLogPaginationQueryAsync(LoggersWithPaginationQuery query)
    {
        var data = await Mediator.Send(query);
        return await Result<PaginatedData<LoggerOutput>>.SucceedAsync(data);
    }

    ///// <summary>
    ///// 清空日志
    ///// </summary>
    ///// <returns></returns>
    //[HttpDelete("clear/{type}/logs")]
    //[AllowAnonymous]
    //[Description("清空日志")]
    //public async Task<Result<bool>> ClearAsync(ClearLoggerCommand command)
    //{
    //    return await Result<bool>.SucceedAsync(await Mediator.Send(command));
    //}
}
